package com.cskaoyan.javase.practice.stage5;

import com.cskaoyan.javase.practice.stage5.entity.Student;
import com.cskaoyan.javase.practice.stage5.entity.StudentList;
import com.cskaoyan.javase.practice.stage5.utils.ScannerUtils;

import java.util.Arrays;

/**
 * @program: little-practice
 * @description: console user interface, print message.
 * @author: Keyu Li
 * @create: 2021-09-06 21:06
 **/

public class ConsoleUI {
    public static final int MENU_CHOICE_RANGE = 7;
    private StudentList studentList;

    public ConsoleUI() {
        this.studentList = new StudentList();
    }

    public ConsoleUI(int capacity) {
        this.studentList = new StudentList(capacity);
    }

    public ConsoleUI(String s) {
        System.out.println(s);
        this.studentList = new StudentList(5);
        studentList.add(new Student(11, "李一", "女", 18, "一班", "13656511179"));
        studentList.add(new Student(2, "王二", "男", 18, "二班", "15856222269"));
        studentList.add(new Student(3, "张三", "女", 48, "三班", "13356533329"));
        studentList.add(new Student(7, "赵四", "女", 88, "九班", "13856500029"));
        studentList.add(new Student(2, "王五", "男", 8, "小班", "13356577723"));
    }


    public static void main(String[] args) {
        ConsoleUI cui = new ConsoleUI("TEST");
        cui.start();
    }

    public void start() {
        while (true) {
            showMenu();
            int choice = ScannerUtils.getMenuChoice(MENU_CHOICE_RANGE);
            switch (choice) {
                case 1:
                    consolePrintStudentList();
                    break;
                case 2:
                    consoleAdd();
                    break;
                case 3:
                    consoleDeleteById();
                    break;
                case 4:
                    consoleUpdateById();
                    break;
                case 5:
                    consoleRetrieveById();
                    break;
                case 6:
                    consoleSort();
                    break;
                case 7:
                default:
                    System.out.print("退出系统！---");
                    if (ScannerUtils.getConfirmFlag()) return;
                    break;
            }
        }
    }

    // 学生列表：
    private void consolePrintStudentList() {
        System.out.println("----------------打印学生列表-------------");
        Student[] printStu = studentList.printList();
        if (printStu == null) {
            System.out.println("空列表！");
            return;
        }
        System.out.println("现有学生数：" + printStu.length);
        for (Student stu : printStu) {
            stu.printStu();
        }
    }

    // 增加学生：
    private void consoleAdd() {
        System.out.println("----------------添加学生----------------");
        int id = ScannerUtils.getIdByScanner();
        while (!studentList.notExistId(id)) {
            System.out.println("id已存在，请重新输入！");
            id = ScannerUtils.getIdByScanner();
        }
        studentList.add(ScannerUtils.getInstanceWithIdByScanner(id));
    }

    // 删除学生：
    private void consoleDeleteById() {
        System.out.println("----------------删除学生----------------");
        boolean flag = true;
        while (flag) {
            int id = ScannerUtils.getIdByScanner();
            flag = studentList.deleteById(id);
            if (flag) System.out.println("删除成功！");
            else System.out.println("删除失败！");
            System.out.print("继续删除？----");
            flag = ScannerUtils.getConfirmFlag();
        }
    }

    // 修改学生：
    private void consoleUpdateById() {
        System.out.println("--------更新学生信息（默认不更改id）-------");

        while (true) {
            int id = ScannerUtils.getIdByScanner();
            if (studentList.notExistId(id))
                System.out.print("所查询id不存在! ");
            else studentList.updateById(id, ScannerUtils.getInstanceWithIdByScanner(id));
            System.out.println("继续查找？----");
            if (!ScannerUtils.getConfirmFlag())
                return;
        }

    }

    // 查询学生：
    private void consoleRetrieveById() {
        System.out.println("----------------查询学生信息-------------");
        while (true) {
            int id = ScannerUtils.getIdByScanner();
            Student stu = studentList.findById(id);
            if (stu == null) System.out.println("查找失败！");
            else stu.printStu();
            System.out.print(" 继续查找？----");
            boolean flag = ScannerUtils.getConfirmFlag();
            if (!flag) break;
        }
    }

    // 排序学生
    private void consoleSort(){
        while (true) {
            System.out.println("------------------排序学生---------------");
            System.out.println();
            System.out.println("              1.年 龄 排 序");
            System.out.println("              2.学 号 排 序");
            System.out.println("              3.综 合 排 序");
            System.out.println("              4. 返回主菜单");
            int choice = ScannerUtils.getMenuChoice(4);
            if (choice==4) return;
            if(!studentList.sortByChoice(choice)) System.out.print("排序失败！ ");
            else System.out.print("排序成功！ ");
            System.out.println("是否返回主菜单？----");
            if (ScannerUtils.getConfirmFlag())  return;
        }
    }

    private void showMenu() {
        System.out.println();
        System.out.println("-------------王道Java学生管理系统---------");
        System.out.println();
        System.out.println("              1.学 生 列 表");
        System.out.println("              2.增 加 学 生");
        System.out.println("              3.删 除 学 生");
        System.out.println("              4.修 改 学 生");
        System.out.println("              5.查 询 学 生");
        System.out.println("              6.排 序 学 生");
        System.out.println("              7.退 出 系 统");
    }
}
